package com.example.demo.controller;

import com.example.demo.service.RedisPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: RedisController
 * @Auther: zhangyingqi
 * @Date: 2018/8/29 16:15
 * @Description:
 */
@RestController
public class RedisController {

    private final Logger logger = LoggerFactory.getLogger(RedisController.class);

    @Resource(name="${spring.redis.pool.type}")
    RedisPool redisPool;

    @GetMapping("/redis/str")
    public String testString(){
        String res = redisPool.set(0,"zengjun","男");
        System.out.println("res==============================="+res);
        return redisPool.get(0,"zengjun");
    }

    @GetMapping("/redis/hash")
    public void testHash(){
        Map<String,String> map = new HashMap<String,String>();
        map.put("gender","男");
        map.put("birthday","2019-07-18");
        map.put("createTime","2019-07-18 19:37:53");
        map.put("age","100");

        String res = redisPool.hmset(0,"zengjun1990",map);
        System.out.println("res==============================="+res);
        redisPool.hincrBy(0,"zengjun1990","age",1);
        Map<String,String> resMap = redisPool.hgetall(0,"zengjun1990");
        for(Map.Entry<String,String> entry : resMap.entrySet()){
            System.out.println(entry.getKey()+" : "+ entry.getValue());
        }
        System.out.println("==================="+redisPool.hget(0,"zengjun1990","gender"));
        redisPool.hset(0,"zengjun1990","gender","女");
        System.out.println("==================="+redisPool.hget(0,"zengjun1990","gender"));
    }


    @GetMapping("/redis/list")
    public void operateList(){
        redisPool.del(0,"zengjun2019");
        System.out.println(redisPool.lset(0,"zengjun2019",0,"100000"));
        redisPool.lpush(0,"zengjun2019","10001","10002","10003","10004","10005","10006");
        redisPool.lpop(0,"zengjun2019"); // 10001 10002 10003 10004 10005
        redisPool.ltrim(0,"zengjun2019",0,2);// 10003 10004 10005
        redisPool.lset(0,"zengjun2019",0,"15555"); // 10003 10004 15555
        System.out.println(redisPool.lindex(0,"zengjun2019",0));//15555
        redisPool.lrange(0,"zengjun2019",0,-1).forEach(
                s-> System.out.println("=================================="+s)
        );
    }

    @GetMapping("/redis/zset")
    public void operateZSet(){
        redisPool.zadd(0,"zengjun0719",1,"zhangsan");
        redisPool.zadd(0,"zengjun0719",2,"lisi");
        redisPool.zadd(0,"zengjun0719",3,"wangwu");
        System.out.println(redisPool.zincrby(0,"zengjun0719",1,"wangwu")); //4.0
        redisPool.zadd(0,"zengjun0719",0,"wangwu");
        redisPool.zrevrangeWithScores(0,"zengjun0719",0,-1).forEach(
                s-> System.out.println("=================================="+s.getElement()+":"+s.getScore())
        );
    }
 
}